#include <IRremote.h>
int RECV_PIN = 11;//红外接收端口
IRrecv irrecv(RECV_PIN);
decode_results results;//结构声明

//==============================
int Left_motor_back=5;     //左电机后退(IN1)
int Left_motor_go=6;     //左电机前进(IN2)

int Right_motor_go=9;    // 右电机前进(IN3)
int Right_motor_back=10;    // 右电机后退(IN4)

void setup()
{
  //初始化电机驱动IO为输出方式
  pinMode(Left_motor_go,OUTPUT); // PIN 5 (PWM)
  pinMode(Left_motor_back,OUTPUT); // PIN 6 (PWM)
  pinMode(Right_motor_go,OUTPUT);// PIN 9 (PWM) 
  pinMode(Right_motor_back,OUTPUT);// PIN 10 (PWM)
  pinMode(13, OUTPUT);////端口模式，输出
  Serial.begin(9600);   //波特率9600
  irrecv.enableIRIn(); // Start the receiver
}
void run()     // 前进
{
  digitalWrite(Right_motor_back,HIGH);  // 右电机前进
  digitalWrite(Right_motor_go,LOW);     
  digitalWrite(Left_motor_go,HIGH);  // 左电机前进
  digitalWrite(Left_motor_back,LOW);
}

void brake()         //刹车，停车
{
  digitalWrite(Right_motor_back,LOW);
  digitalWrite(Right_motor_go,LOW);
  digitalWrite(Left_motor_go,LOW);
  digitalWrite(Left_motor_back,LOW);
}

void left()         //左转(左轮不动，右轮前进)
{
  digitalWrite(Right_motor_back,HIGH);    // 右电机前进
  digitalWrite(Right_motor_go,LOW);
  digitalWrite(Left_motor_go,LOW);   //左轮不动
  digitalWrite(Left_motor_back,LOW);
}

void spin_left()         //左转(左轮后退，右轮前进)
{
  digitalWrite(Right_motor_back,HIGH);    // 右电机前进
  digitalWrite(Right_motor_go,LOW);
  digitalWrite(Left_motor_go,LOW);   //左轮后退
  digitalWrite(Left_motor_back,HIGH);
}

void right()        //右转(右轮不动，左轮前进)
{
  digitalWrite(Right_motor_back,LOW);   //右电机不动
  digitalWrite(Right_motor_go,LOW);
  digitalWrite(Left_motor_go,HIGH);//左电机前进
  digitalWrite(Left_motor_back,LOW);
}

void spin_right()        //右转(右轮后退，左轮前进)
{
  digitalWrite(Right_motor_back,LOW);   //右电机后退
  digitalWrite(Right_motor_go,HIGH);
  digitalWrite(Left_motor_go,HIGH);//左电机前进
  digitalWrite(Left_motor_back,LOW);
}

void back()          //后退
{
  digitalWrite(Right_motor_back,LOW);  //右轮后退
  digitalWrite(Right_motor_go,HIGH);
  digitalWrite(Left_motor_go,LOW);  //左轮后退
  digitalWrite(Left_motor_back,HIGH);
}

//=============================================================================
//读取各个按键需要用到这段代码
//=============================================================================

void read_key()
{
    Serial.print("Waiting for signal");
    if(irrecv.decode(&results)){  //如果接收到信息
        Serial.print("code:");
        Serial.println(results.value,HEX);//results.value为16进制,unsigned long
        Serial.print("bits:");
        Serial.println(results.bits);//输出元位数
        irrecv.resume();
      } 
}



void loop()
{
 read_key();
  if(irrecv.decode(&results)){  //如果接收到信息
   switch(results.value){
     case 0xFD8877:  //前,对应2
       run();
       break;
     case 0xFD9867:  //后，
       back();
       break;
     case 0xFD28D7:  //左
       left();
       break;
     case 0xFD6897:  //右
       right();
       break;
     case 0xFDA857:  //停止
       brake();
       break;
     default:
       break;
   }
  irrecv.resume();
  }
}